import os
import django
#os.environ["DJANGO_SETTINGS_MODULE"]="mysite.settings"
#django.setup()

import requests
import re
import _thread
import threading

import glob
from .models import *

from .qdnovel_class import QdNovelClass

class QdDownload:

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
        }

    def __init__(self,
                 book_url,
                 charset = 'utf-8',
                 thread_num = 15,
                 chapter_conurl = 'https://read.qidian.com/chapter/',
                 chapter_start = None,
                 ):
        self.book_url = book_url
        self.charset = charset
        self.chapter_conurl = chapter_conurl
        self.thread_num = thread_num
        
        self.chapter_start = chapter_start
            
        self.book_html = self.get_html(self.book_url)
        self.book_message = self.book_mes()
        self.book_name = self.book_message['book_name'][0]
        self.qd_novel_obj = QdNovel.objects.get(novel_name = self.book_name)
        self.book_author = self.book_message['author_name'][0]
        self.book_status = self.book_message['book_status'][0]
        self.book_intro = self.book_message['book_intro'][0]
        self.book_detail = self.book_message['book_detail'][0]
        self.chapter_inf = self.book_message['chapter_inf']
        self.free_num = self.book_message['free_num']
        self.info_iter = self.chapter_mes()
        self.thread_pool = []
        print(self.info_iter)
        #if not glob.glob(self.book_name):
            #os.mkdir(self.book_name)

    def get_html(self,now_url):  #这个方法用来传入一个链接，获取HTML内容
        
        res = requests.get(now_url,headers = self.headers)
        res.encoding = self.charset
        return res.text
    
    def get_info(self,html_info,**regex):   #传入网页源代码，在里面查找符合正则表达式的结果并以字典返回
        
        info_dict = {}
        for key,value in regex.items():
            info_dict[key] = re.findall(value,html_info,re.S)

        return info_dict
     
    def book_mes(self,           #获取书本信息，书名，小说状态，作者等
                 author_name = '<p><a href="//my.qidian.com/author/\d*?" target="_blank">(.*?)</a>',
                 book_intro = '<p class="intro">(.*?)</p>',
                 book_status = '<p class="tag"><span class="blue">(.*?)</span>',
                 book_detail = '<div class="book-intro">(.*?)</div>',
                 book_name = '<h1>.*?<em>(.*?)</em>',
                 chapter_inf = '<li data-rid=".*?"><a href=".*?" target="_blank" data-eid="qd_G55" data-cid=".*?" title="(.*?)">.*?</a>',
                 free_num = '</a>.*?<i>&#183;</i>共(.*?)章<span class=free> 免费</span>',
                 ):
        book_h5 = self.book_html
        info_dict = self.get_info(html_info = book_h5,
                                  author_name = author_name,book_intro = book_intro,
                                  book_status = book_status,book_detail = book_detail,
                                  book_name = book_name,chapter_inf =chapter_inf,
                                  free_num = free_num
                                  )
        
        free_num_list = info_dict['free_num']
        free_num = 0
        for i in free_num_list:
            free_num += int(i)
        info_dict['free_num'] = free_num
        print('*'*100)
        print('总数在这儿：',free_num)
        print('*'*100)
        return info_dict

    def chapter_mes(self,    #获取章节信息，章节名，章节链接，章节信息，总章节数
                    chapter_name = '<li data-rid=".*?"><a href=".*?" target="_blank" data-eid="qd_G55" data-cid=".*?" title=".*?">(.*?)</a>',
                    chapter_url = '<li data-rid=".*?"><a href="//read.qidian.com/chapter/(.*?)" target="_blank" data-eid="qd_G55" data-cid=".*?" title=".*?">.*?</a>',
                    #chapter_info = '<li data-rid=".*?"><a href=".*?" target="_blank" data-eid="qd_G55" data-cid=".*?" title="(.*?)">.*?</a>',
                    #free_num = '</a>.*?<i>&#183;</i>共(.*?)章<span class=free> 免费</span>',
                    #chapter_name = '<li data-rid=".*?"><a href="//read.qidian.com/chapter/(.*?)" target="_blank" data-eid="qd_G55" data-cid=".*?" title=".*?">(.*?)</a>',
                    ):
        book_h5 = self.book_html
        chapter_name_list = re.findall(chapter_name,book_h5,re.S)
        chapter_url_list = re.findall(chapter_url,book_h5,re.S)
        
        qdnovel_obj = QdNovel.objects.get(novel_name = self.book_name)
        qdnovel_info_count = qdnovel_obj.chapterinfo_set.count()
        
        if qdnovel_info_count < self.free_num:
            new_name_list = chapter_name_list[qdnovel_info_count:]
            print('新的章节名的长度',len(new_name_list))
            
            new_url_list = chapter_url_list[qdnovel_info_count:]
            print('新的链接的长度',len(new_url_list))
            chapter_list = []
            for i in range(len(new_url_list)):
                chapter_list.append((new_url_list[i],new_name_list[i]))
            
            return enumerate(iter(chapter_list),qdnovel_info_count)

        else:
            
            chapter_list = []
            for i in range(len(chapter_name_list)):
                chapter_list.append((chapter_url_list[i],chapter_name_list[i]))
            
            return enumerate(iter(chapter_list))

        '''   
        book_h5 = self.book_html
        chapter_name_list = re.findall(chapter_name,book_h5,re.S)[self.chapter_start:]
        chapter_url_list = re.findall(chapter_url,book_h5,re.S)[self.chapter_start:]
        
        chapter_list = []
        for i in range(len(chapter_name_list)):
            chapter_list.append((chapter_url_list[i],chapter_name_list[i]))
            
        return enumerate(iter(chapter_list))

        #return enumerate(re.finditer(chapter_url,book_h5))
        '''
    def detail_content(self,
                       cha_url,
                       detail_word = '<i><em class="iconfont">&#xe64f;</em><span class="j_chapterWordCut">(.*?)</span>字</i>',
                       detail_update = '<i><em class="iconfont">&#xe653;</em><span class="j_updateTime">(.*?)</span></i>',
                       detail_pre = '<a id="j_chapterPrev" data-eid="qd_R107"    href="//read.qidian.com/chapter/(.*?)">上一章</a><span>\|</span>',
                       detail_cat = '<div class="chapter-control dib-wrap" data-l1="3">.*?<a href="//book.qidian.com/info/(.*?)" target="_blank" data-eid="qd_R108">目录</a>.*?</div>',
                       detail_next = '<a id="j_chapterNext" href="//read.qidian.com/chapter/(.*?)" data-eid="qd_R109" >下一章</a>',
                       detail_reg = '<div class="read-content j_readContent">(.*?)</div>',
                       ):
        #cha_html = self.get_html(cha_url)
        cha_obj = requests.get(cha_url,headers = self.headers)
        cha_obj.encoding = self.charset
        cha_html = cha_obj.text

        cha_word = re.findall(detail_word,cha_html,re.S)[0]
        cha_update = re.findall(detail_update,cha_html,re.S)[0]
        
        cha_cat = re.findall(detail_cat,cha_html,re.S)[0]
        cha_next = re.findall(detail_next,cha_html,re.S)
        if len(cha_next) == 0:
            cha_next = ''
        else:
            cha_next = cha_next[0]
        cha_pre = re.findall(detail_pre,cha_html,re.S)
        if len(cha_pre) == 0:
            cha_pre = ''
        else:
            cha_pre = cha_pre[0]
        cha_content = re.findall(detail_reg,cha_html,re.S)[0]
        
        return cha_content,cha_word,cha_update,cha_pre,cha_cat,cha_next

    def save_file(self):
        
        #self.qd_novel_obj.chapterinfo_set.all()
        while True:
            print(threading.active_count())
            try:
                chapter_info = next(self.info_iter)
                print(chapter_info)

            except StopIteration:

                if threading.active_count() == 3:
                    #last_content = self.joint_file()
                    
                    return self.qd_novel_obj.novel_name

                else:
                    
                    return self.qd_novel_obj.novel_name

            else:
                last_url = self.chapter_conurl + chapter_info[1][0]

                print('last_url',last_url)
                print(chapter_info[1][1])
                
                times = chapter_info[0]
                print('现在的迭代次数',times)
                
                cha_info = self.detail_content(last_url)
                    
                #在这儿往数据库里写每一章的内容
                self.qd_novel_obj.chapterinfo_set.create(chapter_num = chapter_info[0],
                                                         chapter_ranking = chapter_info[1][1],
                                                         chapter_url = chapter_info[1][0],
                                                         chapter_info = cha_info[0],
                                                         chapter_word = cha_info[1],
                                                         chapter_update = cha_info[2],
                                                         chapter_pre = cha_info[3],
                                                         chapter_cat = cha_info[4],
                                                         chapter_next = cha_info[5],
                                                         )
                
                if threading.active_count() < self.thread_num:
                    self.duo_xian()
    def joint_file(self):

        t_num = len(glob.glob(self.book_name + '/*.txt'))
        
        with open('{}/书名：{}--作者：{}--连载：{}.txt'.format\
                  (self.book_name,self.book_name,self.book_author,self.book_status),'a') as y:
            y.write(self.book_intro)
            y.write('\n\n')
            y.write(self.book_detail)
            y.write('\n\n')
            y.write('总章节：{}'.format(self.free_num))
            y.write('\n\n')
      
        for i in range(t_num):
            with open('{}/书名：{}--作者：{}--连载：{}.txt'.format\
                      (self.book_name,self.book_name,self.book_author,self.book_status),'a') as f:
                with open('{}/{}.txt'.format(self.book_name,i)) as ff:
                    f.write(ff.read())
                os.remove('{}/{}.txt'.format(self.book_name,i))
        with open('{}/书名：{}--作者：{}--连载：{}.txt'.format\
                      (self.book_name,self.book_name,self.book_author,self.book_status),'r') as r:
            last_content = r.read()
            #print(last_content)
        return last_content
            
    def duo_xian(self):

        for u in range(self.thread_num):
            x = threading.Thread(target = self.save_file,args = ())
            x.start()
            self.thread_pool.append(x)
        for i in self.thread_pool:
            i.join()

if __name__ == '__main__':
    a = QdDownload('https://book.qidian.com/info/1759532')
    c = a.save_file()
    print(c)



        
